package com.mall.business.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.business.domain.TCashbackRule;
import com.mall.business.domain.TOrder;
import com.mall.business.domain.YqFxDTO;
import com.mall.business.service.ITCashbackRuleService;
import com.mall.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.business.mapper.TYqUserMapper;
import com.mall.business.domain.TYqUser;
import com.mall.business.service.ITYqUserService;
import org.springframework.util.CollectionUtils;

/**
 * 邀请人Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-28
 */
@Service
public class TYqUserServiceImpl extends ServiceImpl<TYqUserMapper, TYqUser> implements ITYqUserService {
    @Autowired
    private TYqUserMapper tYqUserMapper;

    /**
     * 查询邀请人
     *
     * @param id 邀请人主键
     * @return 邀请人
     */
    @Override
    public TYqUser selectTYqUserById(String id) {
        return tYqUserMapper.selectTYqUserById(id);
    }

    /**
     * 查询邀请人列表
     *
     * @param tYqUser 邀请人
     * @return 邀请人
     */
    @Override
    public List<TYqUser> selectTYqUserList(TYqUser tYqUser) {
        return tYqUserMapper.selectTYqUserList(tYqUser);
    }

    /**
     * 新增邀请人
     *
     * @param tYqUser 邀请人
     * @return 结果
     */
    @Override
    public int insertTYqUser(TYqUser tYqUser) {
        tYqUser.setCreateTime(DateUtils.getNowDate());
        return tYqUserMapper.insertTYqUser(tYqUser);
    }

    /**
     * 修改邀请人
     *
     * @param tYqUser 邀请人
     * @return 结果
     */
    @Override
    public int updateTYqUser(TYqUser tYqUser) {
        tYqUser.setUpdateTime(DateUtils.getNowDate());
        return tYqUserMapper.updateTYqUser(tYqUser);
    }

    /**
     * 批量删除邀请人
     *
     * @param ids 需要删除的邀请人主键
     * @return 结果
     */
    @Override
    public int deleteTYqUserByIds(String[] ids) {
        return tYqUserMapper.deleteTYqUserByIds(ids);
    }

    /**
     * 删除邀请人信息
     *
     * @param id 邀请人主键
     * @return 结果
     */
    @Override
    public int deleteTYqUserById(String id) {
        return tYqUserMapper.deleteTYqUserById(id);
    }


    @Autowired
    private ITCashbackRuleService tCashbackRuleService;



    @Override
    public YqFxDTO getFxMoney(String byquer, BigDecimal totalPrice) {
        YqFxDTO yqFxDTO = new YqFxDTO();

        BigDecimal fxPrice = BigDecimal.ZERO;
        if (totalPrice.compareTo(BigDecimal.ZERO) > 0) {
            //邀请用户返现
            TYqUser yqUser = new TYqUser();
            yqUser.setBUserId(byquer);
            List<TYqUser> tYqUsers = this.selectTYqUserList(yqUser);
            if (!CollectionUtils.isEmpty(tYqUsers)) {
                String yUserId = tYqUsers.get(0).getYUserId();
                yqFxDTO.setYquserId(yUserId);
                TYqUser queryq = new TYqUser();
                queryq.setYUserId(yUserId);
                List<TYqUser> tYqUsers2 = this.selectTYqUserList(queryq);
                // 邀请人数
                Integer yqNum = tYqUsers2.size();
                List<TCashbackRule> tCashbackRules = tCashbackRuleService.selectTCashbackRuleList(null);
                for (TCashbackRule tCashbackRule : tCashbackRules) {
                    if (yqNum >= tCashbackRule.getMinInvite() && yqNum <= tCashbackRule.getMaxInvite()) {
                        //返现比例
                        BigDecimal cashbackRatio = tCashbackRule.getCashbackRatio();
                        if (cashbackRatio.compareTo(BigDecimal.ZERO) > 0) {
                            //返现比例
                            fxPrice = totalPrice.multiply(cashbackRatio);
                        }
                    }
                }
            }
        }
        yqFxDTO.setByquserId(byquer);
        yqFxDTO.setFxMoney(fxPrice);
        return yqFxDTO;
    }






}
